package com.aurora.oa.login.view;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.design.widget.TextInputEditText;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatButton;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;

import com.aurora.oa.Constants.AuroraConstants;
import com.aurora.oa.R;
import com.aurora.oa.activity.MainActivity;
import com.aurora.oa.customview.ScrollTextView;
import com.aurora.oa.listener.OnRequestHttpDataListener;
import com.aurora.oa.login.presenter.LoginPresenter;
import com.aurora.oa.model.RequestHttpModel;
import com.aurora.oa.model.RequestHttpModelImpl;
import com.aurora.oa.receiver.NetworkStateChangeReceiver;
import com.facebook.drawee.view.SimpleDraweeView;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/3/10 0010.
 */
public class ActivityLogin  extends AppCompatActivity implements ILoginView{

    private static final String TAG = "ActivityLogin";
    private TextInputEditText et_username;
    private TextInputEditText et_password;
    private AppCompatButton bt_login;
    private LoginPresenter mLoginPresenter;
    private ProgressDialog progressDialog;
    public static final String CONNECTIVITY_CHANGE_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initData();
        initView();
      //  registBroadCast();


    }

    private void registBroadCast() {
            Log.i(TAG, "register receiver " + CONNECTIVITY_CHANGE_ACTION);
            IntentFilter filter = new IntentFilter();
            filter.addAction(CONNECTIVITY_CHANGE_ACTION);
            filter.setPriority(1000);
            registerReceiver(new NetworkStateChangeReceiver(), filter);

    }


    private void initData() {
        mLoginPresenter = new LoginPresenter(this,this);
    }

    //初始化控件
    private void initView() {
        final ScrollTextView scrollTextView = (ScrollTextView)findViewById(R.id.scroll);
        et_username = (TextInputEditText) findViewById(R.id.et_username);
        et_password = (TextInputEditText) findViewById(R.id.et_password);
        bt_login = (AppCompatButton) findViewById(R.id.bt_login);
        bt_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //登录交给LoginPresenter处理
               mLoginPresenter.doLogin(et_username.getText().toString(),et_password.getText().toString());
            }
        });
        mLoginPresenter.showLoginInfo();
    }


    @Override
    public void onLoginSuccess() {
        startActivity(new Intent(ActivityLogin.this, MainActivity.class));
        mLoginPresenter.saveLoginInfo(et_username.getText().toString(),et_password.getText().toString());
       // finish();
    }

    @Override
    public void onLoginFail(String loginInfo) {
        Toast.makeText(this,"登录失败"+loginInfo,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showProgress() {

        progressDialog = ProgressDialog.show(this,null,"正在登录中...");
    }

    @Override
    public void hideProgress() {
        progressDialog.dismiss();
    }

    @Override
    public void showLoginInfo(String userName, String passWord) {
        et_username.setText(userName);
        et_password.setText(passWord);
    }

}
